package com.wyzz.hr.util;

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.sql.Timestamp;
import java.text.*;
import java.util.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.axis.utils.StringUtils;

public class MyTools {

	public MyTools() {
	}

	public static String nullToStr(String inString, String newString) {
		if (inString == null && newString != null)
			return newString;
		else
			return inString;
	}

	public static String dateToFormat(Date inputeDate) {
		DateFormat df1 = DateFormat.getDateInstance(1);
		DateFormat df2 = DateFormat.getTimeInstance(2);
		return (new StringBuilder()).append(df1.format(inputeDate)).append(
				df2.format(inputeDate)).toString();
	}

	public static String isotogb(String ori) {
		String newstr = "";
		try {
			if (ori == null)
				ori = "";
			byte byte1[] = null;
			byte1 = ori.getBytes("ISO-8859-1");
			newstr = new String(byte1, "EUC_CN");
			return newstr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newstr;
	}

	public static String isotoutf8(String ori) {
		String newstr = "";
		try {
			if (ori == null)
				ori = "";
			byte byte1[] = null;
			byte1 = ori.getBytes("ISO-8859-1");
			newstr = new String(byte1, "utf-8");
			return newstr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newstr;
	}

	public static String gbtoiso(String ori) {
		String newstr = "";
		try {
			if (ori == null)
				ori = "";
			byte byte1[] = null;
			byte1 = ori.getBytes("EUC_CN");
			newstr = new String(byte1, "ISO-8859-1");
			return newstr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newstr;
	}

	public static String filtertag(String src) {
		if (src == null) {
			return "";
		} else {
			String result = "";
			result = insteadstr(src, "&", "&amp;");
			result = insteadstr(result, "<", "&lt;");
			result = insteadstr(result, ">", "&gt;");
			result = insteadstr(result, "\n", "<BR>");
			result = insteadstr(result, "\"", "&quot;");
			return result;
		}
	}

	public static String getcurrdate() {
		Date currDate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String strdate = sdf.format(currDate);
		return strdate;
	}

	public static String getcurrdate(String style) {
		Date currDate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(style);
		String strdate = sdf.format(currDate);
		return strdate;
	}

	public static Timestamp getTimestamp(String st) {
		String dataArr[] = st.split("-");
		int year = Integer.parseInt(dataArr[0]);
		int month = Integer.parseInt(dataArr[1]) - 1;
		int day = Integer.parseInt(dataArr[2]);
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		long millis = calendar.getTime().getTime();
		Timestamp ts = new Timestamp(millis);
		return ts;
	}

	public static String insteadstr(String inString, String oldPattern,
			String newPattern) {
		if (inString == null)
			return null;
		if (oldPattern == null || newPattern == null)
			return inString;
		StringBuffer sbuf = new StringBuffer();
		int pos = 0;
		int index = inString.indexOf(oldPattern);
		int patLen = oldPattern.length();
		for (; index >= 0; index = inString.indexOf(oldPattern, pos)) {
			sbuf.append(inString.substring(pos, index));
			sbuf.append(newPattern);
			pos = index + patLen;
		}

		sbuf.append(inString.substring(pos));
		return sbuf.toString();
	}

	public static Number parseNumber(String text, Class targetClass) {
		String trimmed = text.trim();
		if (targetClass.equals(java.lang.Byte.class))
			return Byte.decode(trimmed);
		if (targetClass.equals(java.lang.Short.class))
			return Short.decode(trimmed);
		if (targetClass.equals(java.lang.Integer.class))
			return Integer.decode(trimmed);
		if (targetClass.equals(java.lang.Long.class))
			return Long.decode(trimmed);
		if (targetClass.equals(java.math.BigInteger.class))
			return decodeBigInteger(trimmed);
		if (targetClass.equals(java.lang.Float.class))
			return Float.valueOf(trimmed);
		if (targetClass.equals(java.lang.Double.class))
			return Double.valueOf(trimmed);
		if (targetClass.equals(java.math.BigDecimal.class)
				|| targetClass.equals(java.lang.Number.class))
			return new BigDecimal(trimmed);
		else
			throw new IllegalArgumentException((new StringBuilder()).append(
					"\u65E0\u6CD5\u8F6C\u6362\u5B57\u7B26\u4E32  [").append(
					text).append("] \u5230\u76EE\u6807\u7C7B\u578B [").append(
					targetClass.getName()).append("]").toString());
	}

	private static BigInteger decodeBigInteger(String value) {
		int radix = 10;
		int index = 0;
		boolean negative = false;
		if (value.startsWith("-")) {
			negative = true;
			index++;
		}
		if (value.startsWith("0x", index) || value.startsWith("0X", index)) {
			index += 2;
			radix = 16;
		} else if (value.startsWith("#", index)) {
			index++;
			radix = 16;
		} else if (value.startsWith("0", index) && value.length() > 1 + index) {
			index++;
			radix = 8;
		}
		BigInteger result = new BigInteger(value.substring(index), radix);
		return negative ? result.negate() : result;
	}

	public static String moneyLowToUpper(double param) {
		String Dw[] = { "\u5206", "\u89D2", "\u5143", "\u62FE", "\u4F70",
				"\u4EDF", "\u4E07", "\u62FE", "\u4F70", "\u4EDF", "\u4EBF",
				"\u62FE", "\u4F70", "\u4EDF", "\u5146", "\u62FE", "\u4F70",
				"\u4EDF" };
		String Num[] = { "\u96F6", "\u58F9", "\u8D30", "\u53C1", "\u8086",
				"\u4F0D", "\u9646", "\u67D2", "\u634C", "\u7396" };
		String str = (new StringBuilder()).append((int) (param * 100D)).append(
				"").toString();
		int length = str.length();
		char c[] = new char[length];
		int character_value[] = new int[length];
		String depart = "";
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			c[i] = str.charAt(i);
			character_value[i] = Integer.parseInt((new StringBuilder()).append(
					c[i]).append("").toString());
		}

		for (int i = 0; i < length; i++) {
			if (character_value[i] == 0) {
				if (i == length - 1)
					depart = "";
				else if (i == length - 2) {
					if (Integer.parseInt((new StringBuilder()).append(
							c[length - 1]).append("").toString()) == 0)
						depart = "\u6574";
					else
						depart = Num[character_value[i]];
				} else if (i == length - 3)
					depart = Dw[length - 1 - i];
				else if (i == length - 7 || i == length - 11)
					depart = (new StringBuilder()).append(Dw[length - 1 - i])
							.append(Num[character_value[i]]).toString();
				else if (Integer.parseInt((new StringBuilder())
						.append(c[i - 1]).append("").toString()) == 0)
					depart = "";
				else if (i == length - 4) {
					if (Integer.parseInt((new StringBuilder()).append(
							c[length - 3]).append("").toString()) == 0)
						depart = "";
					else
						depart = Num[character_value[i]];
				} else {
					depart = Num[character_value[i]];
				}
			} else {
				depart = (new StringBuilder()).append(Num[character_value[i]])
						.append(Dw[length - 1 - i]).toString();
			}
			sb.append(depart);
		}

		return sb.toString();
	}

	public static String trim(String inString, String pattern) {
		return insteadstr(inString, pattern, "");
	}

	public static String num2show(double n) {
		String s = "";
		if (n == 0.0D) {
			return "&nbsp;";
		} else {
			s = DecimalFormat.getInstance().format(n);
			return strFill(s);
		}
	}

	public static String num2show(float n) {
		String s = "";
		if (n == 0.0F) {
			return "&nbsp;";
		} else {
			s = DecimalFormat.getInstance().format(n);
			return strFill(s);
		}
	}

	public static String num2show(long n) {
		String s = "";
		if (n == 0L) {
			return "&nbsp;";
		} else {
			s = DecimalFormat.getInstance().format(n);
			return s;
		}
	}

	public static String num2show(int n) {
		String s = "";
		if (n == 0) {
			return "&nbsp;";
		} else {
			s = DecimalFormat.getInstance().format(n);
			return s;
		}
	}

	public static double round(double f, int n) {
		int r = 1;
		for (int i = 1; i <= Math.abs(n); ++i)
			r *= 10;
		if (n >= 0) {
			return Math.round(f * r) / r;
		}
		return Math.round(f / r) * r;
	}

	public static double floor(double f, int n) {
		int r = 1;
		for (int i = 1; i <= Math.abs(n); ++i)
			r *= 10;
		if (n >= 0) {
			return Math.floor(f * r) / r;
		}
		return Math.floor(f / r) * r;
	}

	public static double ceil(double f, int n) {
		int r = 1;
		for (int i = 1; i <= Math.abs(n); ++i)
			r *= 10;
		if (n >= 0) {
			return Math.ceil(f * r) / r;
		}
		return Math.ceil(f / r) * r;
	}

	public static float round(float f, int n) {
		int r = 1;
		for (int i = 1; i <= Math.abs(n); i++)
			r *= 10;

		if (n >= 0)
			return (float) Math.round(f * (float) r) / (float) r;
		else
			return (float) Math.round(f / (float) r) * (float) r;
	}

	public static String strFill(String s) {
		if (s == null || s.length() == 0)
			return s;
		int index = s.indexOf(".");
		String s1 = "";
		if (index > 0) {
			s1 = s.substring(index);
			if (s1.length() == 1)
				s = (new StringBuilder()).append(s).append("00").toString();
			else if (s1.length() == 2)
				s = (new StringBuilder()).append(s).append("0").toString();
		} else {
			s = (new StringBuilder()).append(s).append(".00").toString();
		}
		return s;
	}

	public static String getStrFromProperties(String propertiesfileName,
			String id) {
		ResourceBundle errBundle = PropertyResourceBundle.getBundle(
				propertiesfileName, new Locale("ch", "CH"));
		String _str = "";
		try {
			_str = errBundle.getString(id);
		} catch (NullPointerException e) {
			_str = "";
		}
		if (_str == null)
			_str = "";
		try {
			_str = new String(_str.getBytes("ISO8859_1"), "GB2312");
		} catch (Exception e) {
			_str = "";
		}
		return _str;
	}

	public static void writeFile(String fileName, String str) {
		PrintStream out = null;
		try {
			File outFile = new File(fileName);
			out = new PrintStream(new BufferedOutputStream(
					new FileOutputStream(outFile)));
			out.println(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.close();
	}

	public static String readFile(String fileName) {
		String s = "";
		StringBuffer sb = new StringBuffer();
		File inFile = new File(fileName);
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(new FileInputStream(
					inFile), "EUC_CN"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			while ((s = in.readLine()) != null)
				sb.append((new StringBuilder()).append(s).append("\n")
						.toString());
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public static String getFileTxt(String filename, String charset) {
		String str = "";
		File phTxt = new File(filename);
		if (phTxt.isFile())
			try {
				FileInputStream phFile = new FileInputStream(phTxt);
				if (phFile != null) {
					InputStreamReader phSr = new InputStreamReader(phFile,
							charset);
					if (phSr != null) {
						BufferedReader phReader = new BufferedReader(phSr);
						if (phReader != null) {
							for (String sbuf = ""; (sbuf = phReader.readLine()) != null;) {
								if (str.length() > 0)
									str = (new StringBuilder()).append(str)
											.append("\r\n").toString();
								str = (new StringBuilder()).append(str).append(
										sbuf).toString();
							}

							phReader.close();
						}
						phSr.close();
					}
					phFile.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		return str;
	}

	public static String madeNbsp(int count) {
		String str = "";
		for (int i = 0; i < count; i++)
			str = (new StringBuilder()).append(str).append("&nbsp;").toString();

		return str;
	}

	public static String arrayToDelimitedString(Object arr[], String delim) {
		if (arr == null || arr.length == 0)
			return "";
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; i++) {
			if (i > 0)
				sb.append(delim);
			sb.append('\'');
			sb.append(arr[i]);
			sb.append('\'');
		}

		return sb.toString();
	}

	public static String arrayToStr(String array1[], char split, char ch) {
		if (array1 == null || array1.length < 0)
			return null;
		String string1 = "";
		int len = array1.length - 1;
		if (!"".equals(Character.valueOf(ch))) {
			for (int i = 0; i < array1.length; i++) {
				string1 = (new StringBuilder()).append(string1).append('\'')
						.toString();
				string1 = (new StringBuilder()).append(string1).append(
						array1[i].toString()).toString();
				string1 = (new StringBuilder()).append(string1).append('\'')
						.toString();
				if (i != len)
					string1 = (new StringBuilder()).append(string1).append(
							split).toString();
			}

		} else {
			for (int i = 0; i < array1.length; i++) {
				string1 = (new StringBuilder()).append(string1).append(
						array1[i].toString()).toString();
				if (i != len)
					string1 = (new StringBuilder()).append(string1).append(
							split).toString();
			}

		}
		return string1;
	}

	public static String arrayToStr(Object arr[], char split) {
		if (arr == null || arr.length == 0)
			return null;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; i++) {
			if (i > 0)
				sb.append(split);
			sb.append(arr[i]);
		}

		return sb.toString();
	}

	public static String[] strSplit(String fieldsru, char tag) {
		char dot = tag;
		if (fieldsru == null || fieldsru.equals(""))
			return null;
		String field;
		if (fieldsru.charAt(fieldsru.length() - 1) != dot)
			field = (new StringBuilder()).append(fieldsru).append(dot)
					.toString();
		else
			field = fieldsru;
		int num = 0;
		int field_len = field.length();
		for (int i = 0; i < field_len; i++)
			if (field.charAt(i) == dot)
				num++;

		int begin = 0;
		String returnStr[] = new String[num];
		for (int j = 0; j < num; j++) {
			int end = field.indexOf(dot, begin);
			returnStr[j] = field.substring(begin, end);
			begin = end + 1;
		}

		return returnStr;
	}

	public static Hashtable StrSpilt(String fieldsru, String tag) {
		Hashtable returnarray = new Hashtable();
		char dot = tag.charAt(0);
		String field = (new StringBuilder()).append(fieldsru).append(dot)
				.toString();
		int num = 0;
		int field_len = field.length();
		for (int i = 0; i < field_len; i++)
			if (field.charAt(i) == dot)
				num++;

		int begin = 0;
		for (int j = 0; j < num; j++) {
			int end = field.indexOf(dot, begin);
			returnarray.put(new Integer(j), field.substring(begin, end));
			begin = end + 1;
		}

		return returnarray;
	}

	public static String toFormat(String s, String i_format) {
		if (i_format == null)
			return s;
		if (i_format.equals("$NUM_CN"))
			return toChineseNumber(s);
		if (i_format.equals("$DEC_2"))
			return toDecmalFormat(s, 2D);
		if (i_format.equals("$DEC_4"))
			return toDecmalFormat(s, 4D);
		if (i_format.equals("$DEC_6"))
			return toDecmalFormat(s, 6D);
		if (i_format.equals("$HTML"))
			return filtertag(s);
		else
			return s;
	}

	public static String PositiveIntegerToHanStr(String NumStr) {
		String HanDigiStr[] = { "\u96F6", "\u58F9", "\u8D30", "\u53C1",
				"\u8086", "\u4F0D", "\u9646", "\u67D2", "\u634C", "\u7396" };
		String HanDiviStr[] = { "", "\u62FE", "\u4F70", "\u4EDF", "\u4E07",
				"\u62FE", "\u4F70", "\u4EDF", "\u4EBF", "\u62FE", "\u4F70",
				"\u4EDF", "\u4E07", "\u62FE", "\u4F70", "\u4EDF", "\u4EBF",
				"\u62FE", "\u4F70", "\u4EDF", "\u4E07", "\u62FE", "\u4F70",
				"\u4EDF" };
		String RMBStr = "";
		boolean lastzero = false;
		boolean hasvalue = false;
		int len = NumStr.length();
		if (len > 15)
			return "\u6570\u503C\u8FC7\u5927!";
		for (int i = len - 1; i >= 0; i--) {
			if (NumStr.charAt(len - i - 1) == ' ')
				continue;
			int n = NumStr.charAt(len - i - 1) - 48;
			if (n < 0 || n > 9)
				return "\u8F93\u5165\u542B\u975E\u6570\u5B57\u5B57\u7B26!";
			if (n != 0) {
				if (lastzero)
					RMBStr = (new StringBuilder()).append(RMBStr).append(
							HanDigiStr[0]).toString();
				if (n != 1 || i % 4 != 1 || i != len - 1)
					RMBStr = (new StringBuilder()).append(RMBStr).append(
							HanDigiStr[n]).toString();
				RMBStr = (new StringBuilder()).append(RMBStr).append(
						HanDiviStr[i]).toString();
				hasvalue = true;
			} else if (i % 8 == 0 || i % 8 == 4 && hasvalue)
				RMBStr = (new StringBuilder()).append(RMBStr).append(
						HanDiviStr[i]).toString();
			if (i % 8 == 0)
				hasvalue = false;
			lastzero = n == 0 && i % 4 != 0;
		}

		if (RMBStr.length() == 0)
			return HanDigiStr[0];
		else
			return RMBStr;
	}

	public static String toChineseNumber(String NumStr) {
		if (NumStr == null)
			return "";
		else
			return toChineseNumber(Double.parseDouble(NumStr));
	}

	public static String toChineseNumber(double val) {
		String HanDigiStr[] = { "\u96F6", "\u58F9", "\u8D30", "\u53C1",
				"\u8086", "\u4F0D", "\u9646", "\u67D2", "\u634C", "\u7396" };
		String HanDiviStr[] = { "", "\u62FE", "\u4F70", "\u4EDF", "\u4E07",
				"\u62FE", "\u4F70", "\u4EDF", "\u4EBF", "\u62FE", "\u4F70",
				"\u4EDF", "\u4E07", "\u62FE", "\u4F70", "\u4EDF", "\u4EBF",
				"\u62FE", "\u4F70", "\u4EDF", "\u4E07", "\u62FE", "\u4F70",
				"\u4EDF" };
		String SignStr = "";
		String TailStr = "";
		if (val < 0.0D) {
			val = -val;
			SignStr = "\u8D1F";
		}
		if (val > 100000000000000D || val < -100000000000000D)
			return "\u6570\u503C\u4F4D\u6570\u8FC7\u5927!";
		long temp = Math.round(val * 100D);
		long integer = temp / 100L;
		long fraction = temp % 100L;
		int jiao = (int) fraction / 10;
		int fen = (int) fraction % 10;
		if (jiao == 0 && fen == 0) {
			TailStr = "\u6574";
		} else {
			TailStr = HanDigiStr[jiao];
			if (jiao != 0)
				TailStr = (new StringBuilder()).append(TailStr)
						.append("\u89D2").toString();
			if (integer == 0L && jiao == 0)
				TailStr = "";
			if (fen != 0)
				TailStr = (new StringBuilder()).append(TailStr).append(
						HanDigiStr[fen]).append("\u5206").toString();
		}
		if (integer < 1L)
			return (new StringBuilder()).append(SignStr).append(TailStr)
					.toString();
		else
			return (new StringBuilder()).append(SignStr).append(
					PositiveIntegerToHanStr(String.valueOf(integer))).append(
					"\u5143").append(TailStr).toString();
	}

	public static String toDecmalFormat(String s, double fraction) {
		if (fraction < 0.0D)
			fraction = 2D;
		s = Double.toString((double) Math.round(Double.parseDouble(s)
				* Math.pow(10D, fraction))
				/ Math.pow(10D, fraction) + Math.pow(10D, -1D - fraction));
		return s.substring(0, s.length() - 1);
	}

	public static String ucs(String s) {
		try {
			return new String(s.getBytes(), "GB2312");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}

	public static void SendRedirect(String myurl, HttpServletRequest request,
			HttpServletResponse response) {
		RequestDispatcher rd = null;
		if (myurl != null && myurl.trim().length() != 0)
			rd = request.getRequestDispatcher(myurl);
		try {
			rd.forward(request, response);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static String computepass(String oripass) {
		String key = "01234567890123456789";
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < oripass.length(); i++) {
			char tt = (char) (oripass.charAt(i) ^ key.charAt(i));
			sb.append(tt);
		}

		return sb.toString();
	}

	public static String cry(String content, String key) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < content.length(); i++) {
			char tt = (char) (content.charAt(i) ^ key.charAt(i));
			sb.append(tt);
		}

		return sb.toString();
	}

	public static String getFromURL(HttpServletRequest request) {
		StringBuffer fromURL = request.getRequestURL();
		Map params = request.getParameterMap();
		if (params != null && params.size() > 0) {
			fromURL.append("?");
			for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
				String key = (String) iter.next();
				String values[] = (String[]) (String[]) params.get(key);
				int i = 0;
				while (i < values.length) {
					fromURL.append(key).append("=").append(values[i]).append(
							"&");
					i++;
				}
			}
		}
		return fromURL.toString();
	}

	public static String getContenByURL(String url) {
		String content = null;
		if (url != null && url.trim().length() > 0)
			try {
				URL myurl = new URL(url);
				BufferedReader in = new BufferedReader(new InputStreamReader(
						myurl.openStream()));
				StringBuffer sb = new StringBuffer();
				String line;
				while ((line = in.readLine()) != null)
					sb.append(line);
				in.close();
				content = sb.toString();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
		return content;
	}

	public static String getValidPhone(String mobile) {
		String validPhone = "";
		if (mobile != null && mobile.trim().length() > 0) {
			if (mobile.indexOf("+") != -1)
				validPhone = mobile.substring(1);
			if (mobile.substring(0, 2).indexOf("86") != -1)
				validPhone = mobile.substring(2);
			if (mobile.substring(0, 2).indexOf("13") != -1
					&& mobile.length() == 11)
				validPhone = mobile;
		}
		return validPhone;
	}

	public static boolean copyFile(String inputFilepathName,
			String onputFilepathName) throws FileNotFoundException, IOException {
		boolean isSuc = false;
		File file_in = new File(inputFilepathName);
		File file_out = new File(onputFilepathName);
		if (copy(new BufferedInputStream(new FileInputStream(file_in)),
				new BufferedOutputStream(new FileOutputStream(file_out))) > 0)
			isSuc = true;
		return isSuc;
	}

	public static int copy(InputStream in, OutputStream out) throws IOException {
		int i;
		int byteCount = 0;
		byte buffer[] = new byte[1024];
		for (int bytesRead = -1; (bytesRead = in.read(buffer)) != -1;) {
			out.write(buffer, 0, bytesRead);
			byteCount += bytesRead;
		}
		out.flush();
		i = byteCount;
		try {
			in.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		try {
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return i;
	}

	public static String getSpId(String mobile) {
		String spid = "";
		if (mobile == null || mobile.length() < 11)
			spid = "invalid mobile";
		else
			try {
				int third = Integer.parseInt(mobile.substring(2, 3));
				if (third < 5)
					spid = "unicom";
				else
					spid = "cmcc";
			} catch (Exception e) {
			}
		return spid;
	}

	public static void runCmd(String cmd) {
		try {
			Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String encode(String str, String charset, String toCharset) {
		if (str != null)
			try {
				if (charset != null)
					str = new String(str.getBytes(charset), toCharset);
				else
					str = new String(str.getBytes(), toCharset);
			} catch (UnsupportedEncodingException uee) {
				uee.printStackTrace();
			}
		return str;
	}

	public static String deleteAny(String inString, String charsToDelete) {
		if (inString == null || charsToDelete == null)
			return inString;
		StringBuffer out = new StringBuffer();
		for (int i = 0; i < inString.length(); i++) {
			char c = inString.charAt(i);
			if (charsToDelete.indexOf(c) == -1)
				out.append(c);
		}

		return out.toString();
	}

	public static String decimalFormat(BigDecimal decimal, int len1, int len2) {
		DecimalFormat decimalFormat = new DecimalFormat("##########0.00");
		if (len1 > 0)
			decimalFormat.setMaximumIntegerDigits(len1);
		if (len2 > 0)
			decimalFormat.setMaximumFractionDigits(len2);
		return decimalFormat.format(decimal);
	}

	public static String vigenere(String str, String key, int mode) {
		if (str == null || key == null)
			return null;
		byte strBytes[] = str.getBytes();
		byte keyBytes[] = key.getBytes();
		int keyLength = keyBytes.length;
		byte result[] = new byte[strBytes.length];
		if (mode == ENCRYPT_MODE) {
			for (int i = 0; i < strBytes.length; i++)
				result[i] = (byte) ((strBytes[i] + keyBytes[i % keyLength]) % 128);

			return new String(result);
		}
		if (mode == DECRYPT_MODE) {
			for (int i = 0; i < strBytes.length; i++) {
				int temp = strBytes[i] - keyBytes[i % keyLength];
				if (temp < 0)
					temp += 128;
				result[i] = (byte) (temp % 128);
			}

			return new String(result);
		} else {
			return null;
		}
	}

	public static String vigenereChinese(String str, String key, int mode) {
		if (str == null || key == null)
			return null;
		if (mode == ENCRYPT_MODE)
			return vigenere(toEncrypt16(str), key, mode);
		if (mode == DECRYPT_MODE)
			return toStringHex(vigenere(str, key, mode));
		else
			return null;
	}

	public static BigDecimal vigenere(BigDecimal bd, String key, int mode) {
		if (bd == null || key == null)
			return null;
		if (mode == ENCRYPT_MODE) {
			String str = bd.toPlainString();
			byte strBytes[] = str.getBytes();
			byte keyBytes[] = key.getBytes();
			int keyLength = keyBytes.length;
			byte result[] = new byte[strBytes.length];
			for (int i = 0; i < strBytes.length; i++)
				if (strBytes[i] == 43 || strBytes[i] == 45 || strBytes[i] == 46)
					result[i] = strBytes[i];
				else
					result[i] = (byte) ((((strBytes[i] - 48) + keyBytes[i
							% keyLength]) - 48) % 10 + 48);

			String inDateBase = new String(result);
			if (!str.substring(0, 1).equals("-"))
				inDateBase = (new StringBuilder()).append("1").append(
						inDateBase).toString();
			else if (str.substring(0, 1).equals("-"))
				inDateBase = (new StringBuilder()).append("2").append(
						inDateBase.substring(1)).toString();
			return new BigDecimal(inDateBase);
		}
		if (mode == DECRYPT_MODE) {
			String str = bd.toPlainString();
			if (str.substring(0, 1).equals("1"))
				str = str.substring(1);
			else
				str = (new StringBuilder()).append("-")
						.append(str.substring(1)).toString();
			byte strBytes[] = str.getBytes();
			byte keyBytes[] = key.getBytes();
			int keyLength = keyBytes.length;
			byte result[] = new byte[strBytes.length];
			for (int i = 0; i < strBytes.length; i++) {
				if (strBytes[i] == 43 || strBytes[i] == 45 || strBytes[i] == 46) {
					result[i] = strBytes[i];
					continue;
				}
				int temp = strBytes[i] - keyBytes[i % keyLength];
				if (temp < 0)
					temp += 10;
				result[i] = (byte) (temp % 10 + 48);
			}

			return new BigDecimal(new String(result));
		} else {
			return null;
		}
	}

	public Object getDecry(Object content, String empNo, int flag) {
		String key = comKeyStr;
		String bd = comKeyBd;
		if (flag == 1) {
			key = vigenere(empNo, comKeyStr, ENCRYPT_MODE);
			byte last[] = empNo.substring(empNo.length() - 1).getBytes();
			BigDecimal tempBd = new BigDecimal(last[0] + 0);
			bd = vigenere(tempBd, comKeyBd, ENCRYPT_MODE).toPlainString();
		}
		try {
			if (content != null
					&& content.getClass().equals(java.lang.String.class))
				content = vigenere((String) content, key, DECRYPT_MODE);
			if (content != null
					&& content.getClass().equals(java.math.BigDecimal.class))
				content = vigenere((BigDecimal) content, bd, DECRYPT_MODE);
			return content;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String toStringHex(String str) {
		byte baKeyword[] = new byte[str.length() / 2];
		for (int i = 0; i < baKeyword.length; i++)
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(str.substring(
						i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
			}

		str = new String(baKeyword);
		return str;
	}

	public static String toEncrypt16(String str) {
		StringBuffer encrypt16 = new StringBuffer("");
		byte bytes[] = str.getBytes();
		for (int i = 0; i < bytes.length; i++) {
			String a = String.format("%02x", new Object[] { Byte
					.valueOf(bytes[i]) });
			encrypt16.append(a);
		}
		str = encrypt16.toString();
		return str;
	}

	public static String getUpKey(String empNo, int flag) {
		if (flag == STRING || flag == CHINESE)
			if (empNo == null || empNo.equals(""))
				return comKeyStr;
			else
				return vigenere(empNo, comKeyStr, ENCRYPT_MODE);
		if (empNo == null || empNo.equals(""))
			return comKeyBd;
		byte last[] = empNo.substring(empNo.length() - 1).getBytes();
		BigDecimal tempBd = new BigDecimal(last[0] + 0);
		String str = tempBd.toPlainString();
		byte strBytes[] = str.getBytes();
		byte keyBytes[] = comKeyBd.getBytes();
		int keyLength = keyBytes.length;
		byte result[] = new byte[strBytes.length];
		for (int i = 0; i < strBytes.length; i++)
			result[i] = (byte) ((((strBytes[i] - 48) + keyBytes[i % keyLength]) - 48) % 10 + 48);
		return new String(result);
	}

	public static Integer getAge(Date birthDate) {
		Date endDate = new Date();
		if (birthDate == null || birthDate.after(endDate))
			return new Integer(0);
		int year = DateUtil.getYear(endDate) - DateUtil.getYear(birthDate);
		int month = DateUtil.getMonth(endDate) - DateUtil.getMonth(birthDate);
		int day = DateUtil.getDay(endDate) - DateUtil.getDay(birthDate);
		if (day < 0)
			month--;
		if (month < 0)
			year--;
		return Integer.valueOf(year);
	}

	public static Float getWorkYearProfile(Date joinDate, Date endDate) {
		if (joinDate == null || joinDate.after(endDate))
			return new Float(0.0D);
		int year = DateUtil.getYear(endDate) - DateUtil.getYear(joinDate);
		int month = DateUtil.getMonth(endDate) - DateUtil.getMonth(joinDate);
		int day = DateUtil.getDay(endDate) - DateUtil.getDay(joinDate);
		if (day < 0)
			month--;
		if (month < 0) {
			month += 12;
			year--;
		}
		Float result = new Float(year);
		result = Float.valueOf(result.floatValue()
				+ (new Float((double) month / 100D)).floatValue());
		return result;
	}

	public static Double getWorkYearSalary(Date joinDate, Date endDate,
			Integer joinYearCalc) {
		if (joinDate.after(endDate) || joinYearCalc == null)
			return new Double(0.0D);
		int year = DateUtil.getYear(endDate) - DateUtil.getYear(joinDate);
		int month = DateUtil.getMonth(endDate) - DateUtil.getMonth(joinDate);
		if (month < 0) {
			month += 12;
			year--;
		}
		if (DateUtil.getDay(joinDate) <= joinYearCalc.intValue()
				&& ++month == 12) {
			month = 0;
			year++;
		}
		Double result = new Double(year);
		result = Double.valueOf(result.doubleValue()
				+ (new Double((double) month / 100D)).doubleValue());
		return result;
	}

	public static double getTaxbyIncome(double income,
			SysConfigVarDef sysConfigVarDef) {
		String taxRange = sysConfigVarDef.getTaxRange();
		String taxRate = sysConfigVarDef.getTaxRate();
		String[] taxRangeArray = taxRange.split(",");
		String[] taxRateArray = taxRate.split(",");
		double base = sysConfigVarDef.getTaxBase().doubleValue();
		double tax_income = round(income - base, 2);
		double[] tax_line = new double[taxRangeArray.length];
		double[] tax_rate = new double[taxRateArray.length];
		for (int i = 0; i < taxRangeArray.length; ++i) {
			tax_line[i] = Double.parseDouble(taxRangeArray[i]);
		}
		for (int i = 0; i < taxRateArray.length; ++i) {
			tax_rate[i] = Double.parseDouble(taxRateArray[i]);
		}
		int i_tax_level = 0;
		while ((i_tax_level < tax_line.length)
				&& (tax_income > tax_line[i_tax_level])) {
			++i_tax_level;
		}
		if (i_tax_level > 0) {
			double result = new Double(0.0D).doubleValue();
			for (int i = 1; i < i_tax_level; ++i) {
				result += (tax_line[i] - tax_line[(i - 1)]) * tax_rate[(i - 1)];
			}
			result += (tax_income - tax_line[(i_tax_level - 1)])
					* tax_rate[(i_tax_level - 1)];
			return round(result, 2);
		}
		return new Double(0.0D).doubleValue();
	}

	public static double getTaxbyLWIncome(double income, Double base) {
		double tax_income = 0.0D;
		double tax_amount = 0.0D;
		if (base == null)
			base = new Double(800D);
		if (income <= 800D)
			return 0.0D;
		if (income <= 4000D)
			tax_income = income - base.doubleValue();
		else
			tax_income = income * 0.80000000000000004D;
		tax_income = round(tax_income, 2);
		if (tax_income > 40000D)
			tax_amount = tax_income * 0.40000000000000002D - 7000D;
		else if (tax_income > 20000D)
			tax_amount = tax_income * 0.29999999999999999D - 2000D;
		else
			tax_amount = tax_income * 0.20000000000000001D;
		return round(tax_amount, 2);
	}

	public static double getTaxbyYearEndReward(float monthSalary,
			float yearEndReward, SysConfigVarDef sysConfigVarDef) {
		Double base = sysConfigVarDef.getTaxBase();
		float mintax_fl = new Float(base.doubleValue()).floatValue();
		String taxRange = sysConfigVarDef.getTaxRange();
		String taxRate = sysConfigVarDef.getTaxRate();
		String[] taxRangeArray = taxRange.split(",");
		String[] taxRateArray = taxRate.split(",");
		float f_income = yearEndReward / 12.0F;
		float[] tax_line = new float[taxRangeArray.length];
		float[] tax_rate = new float[taxRateArray.length];
		for (int i = 0; i < taxRangeArray.length; ++i) {
			tax_line[i] = Float.parseFloat(taxRangeArray[i]);
		}
		for (int i = 0; i < taxRateArray.length; ++i) {
			tax_rate[i] = Float.parseFloat(taxRateArray[i]);
		}
		int i_tax_level = 0;
		while ((i_tax_level < tax_line.length)
				&& (f_income >= tax_line[i_tax_level])) {
			++i_tax_level;
		}
		if (i_tax_level > 0) {
			float result = 0.0F;
			for (int i = 1; i < i_tax_level; ++i) {
				result += (tax_line[i] - tax_line[(i - 1)]) * tax_rate[(i - 1)];
			}
			result = -result
					+ (yearEndReward - ((monthSalary > mintax_fl) ? 0.0F
							: mintax_fl - monthSalary))
					* tax_rate[(i_tax_level - 1)];
			return round(result, 2);
		}
		return 0.0D;
	}

	public static BigDecimal encryDecimal(String empNo, BigDecimal value) {
		if (value == null)
			value = new BigDecimal(0.0D);
		DecimalFormat df = new DecimalFormat("#.00");
		BigDecimal enctrypUse = new BigDecimal(df.format(value));
		String key = getUpKey(empNo, BIGDECIMAL);
		BigDecimal encrypto = vigenere(enctrypUse, key, ENCRYPT_MODE);
		return encrypto;
	}

	public static BigDecimal decryDecimal(String empNo, BigDecimal value) {
		if (value == null) {
			return new BigDecimal(0.0D);
		} else {
			String key = getUpKey(empNo, BIGDECIMAL);
			BigDecimal decrypto = vigenere(value, key, DECRYPT_MODE);
			return decrypto;
		}
	}

	public static String getUUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	public static String unicode2GBK(String unicode) {
		if (StringUtils.isEmpty(unicode))
			return null;
		StringBuffer buf = new StringBuffer(unicode);
		String temp = null;
		Integer charInt = null;
		int i = 0;
		do {
			if (i >= unicode.length())
				break;
			int index = buf.indexOf("\\u", i);
			if (index == -1)
				break;
			buf.replace(index, index + 2, "0x");
			temp = buf.substring(index, index + 6);
			charInt = Integer.decode(temp);
			buf.delete(index, index + 6);
			buf.insert(index, (char) charInt.intValue());
			i++;
		} while (true);
		return buf.toString();
	}

	public static String GBK2unicode(String str) {
		if (StringUtils.isEmpty(str))
			return null;
		String temS = null;
		char str2CharArray[] = str.toCharArray();
		StringBuffer char2Unicode = new StringBuffer();
		for (int i = 0; i < str2CharArray.length; i++) {
			if (!strIsGBK((new StringBuilder()).append("").append(
					str2CharArray[i]).toString())) {
				char2Unicode.append((new StringBuilder()).append("").append(
						str2CharArray[i]).toString());
				continue;
			}
			temS = Integer.toHexString(str2CharArray[i]);
			if (!"a".equals(temS)) {
				if (temS.length() <= 2)
					char2Unicode.append("\\u00").append(temS);
				else
					char2Unicode.append("\\u").append(temS);
			} else {
				char2Unicode.append("\n");
				temS = null;
			}
		}

		return char2Unicode.toString();
	}

	public static boolean strIsGBK(String str) {
		try {
			byte bytes[] = str.getBytes("utf-8");
			if (bytes.length == 2) {
				int ints[] = new int[2];
				ints[0] = bytes[0] & 0xff;
				ints[1] = bytes[1] & 0xff;
				if (ints[0] >= 129 && ints[0] <= 254 && ints[1] >= 64
						&& ints[1] <= 254)
					return true;
			}
		} catch (IOException e) {
			return false;
		}
		return false;
	}

	public static int ENCRYPT_MODE = 0;
	public static int DECRYPT_MODE = 1;
	public static String comKeyStr = "TYHR";
	public static String comKeyBd = "123456789";
	public static String keyStr = "TYHR";
	public static String keyBd = "123456789";
	public static int STRING = 0;
	public static int BIGDECIMAL = 1;
	public static int CHINESE = 2;
	public static final int BUFFER_SIZE = 1024;

}
